package com.atguigu.controller;

import com.atguigu.domain.SysLog;
import com.atguigu.service.UserService;
import com.atguigu.service.sysLogService;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAop {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private sysLogService sysLogService;

    private Date visitTime;//开始时间
    private Class clazz;//哪一个类
    private Method method;//哪一个方法


    //前置通知、主要获取开始时间，执行类是的哪一个，哪一个方法
    @Before("execution(* com.atguigu.controller.*.*(..))")
    public void doBefore(JoinPoint jp) throws Exception {
        visitTime = new Date();
        clazz = jp.getTarget().getClass();
        String name = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        if (args == null || args.length == 0) {
            method = clazz.getMethod(name);
        } else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            clazz.getMethod(name, classArgs);
        }
    }

    //后置通知
    @After("execution(* com.atguigu.controller.*.*(..))")
    public void doAfter(JoinPoint jp) {
        long time = new Date().getTime() - visitTime.getTime();

        String url = "";
        SysLog sysLog = new SysLog();
        //获取url
        if (clazz != null && method != null && clazz != LogAop.class) {
            //1、获取类上的@@RequestMapping("/orders")
            RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (clazzAnnotation != null) {
                String[] clazzValue = clazzAnnotation.value();

                //2、获取方法上的@@RequestMapping("/orders")
                RequestMapping methodAnnotation = (RequestMapping) method.getAnnotation(RequestMapping.class);
                if (clazzAnnotation != null) {
                    String[] methodValue = methodAnnotation.value();
                    url = clazzValue[0] + methodValue[0];
                    //获取访问的ip地址
                    String ip=request.getRemoteAddr();

                    //获取访问的操作用户
                    SecurityContext context = SecurityContextHolder.getContext();
                    User user = (User)context.getAuthentication().getPrincipal();
                    String username = user.getUsername();

                    //把获取的数据封装到SysLog

                    sysLog.setExecutionTime(time);
                    sysLog.setIp(ip);
                    sysLog.setVisitTime(visitTime);
                    sysLog.setUsername(username);
                    sysLog.setUrl(url);
                    sysLog.setMethod("[类名]"+clazz.getName()+"[方法名]"+method.getName());
                    sysLog.setUrl(url);
                    String random = (Math.random()+Math.random()+Math.random() * 1000) + "1";
                    sysLog.setId(random);
                }
            }
        }


        //调用方法存储数据sysLog
        sysLogService.save(sysLog);
    }
}
